import {openConnect} from "@/core/indexDB/dataBase";

const transactionFn = ( dataBaseName:string,tableName:string,cb: any ) => {
    return new Promise( async resolve => {
        await openConnect(dataBaseName,(event:any) => {
            const transaction = event?.transaction(tableName,'readonly')
            const objectStore:IDBObjectStore = transaction.objectStore(tableName)
            const request = cb(objectStore)
            request.onsuccess = (event:Event) => {
                resolve((event.target as IDBOpenDBRequest)?.result)
            }
        })
    })
}

/**
 *  获取数据
 * @param dataBaseName
 * @param tableName
 * @param key
 */
export const getData = async (dataBaseName:string,tableName:string, key?:any) =>
    await transactionFn(dataBaseName, tableName, (objectStore:IDBObjectStore) => objectStore.getAll(key))